package baseproject.com.mybaseproject.mvp.presenter

import baseproject.com.mybaseproject.mvp.contract.LoginContract
import baseproject.com.mybaseproject.net.ApiErrorModel
import baseproject.com.mybaseproject.net.ResultObserver
import baseproject.com.mybaseproject.net.RetrofitManager
import baseproject.com.mybaseproject.utils.scheduler.RxUtils
import com.bybops.app.MyApplication
import com.bybops.model.bean.User
import com.bybops.net.BaseResponse
import com.bybops.utils.AppUtils
import com.bybops.utils.DeviceInfoUtil
import com.bybops.utils.Md5Util

class LoginPresenterImpl : BasePresenterImpl<LoginContract.LoginView>() , LoginContract.LoginPresenter{

    override fun login() {
        var params = HashMap<String , String?>()

        params["telephone"] =  mView?.getUserName()
        params["password"] = Md5Util.encrypt(mView?.getPassword())
        params["telephoneCode"] = ""
        params["checkCode"] = ""
        params["apiVersion"] = AppUtils.getVersionName(MyApplication.instance)
        params["model"] = DeviceInfoUtil.getModel()
        params["osVersion"] = DeviceInfoUtil.getOsVersion()
        params["serial"] = DeviceInfoUtil.getDeviceId()
        mView?.showProgress("正在加载...")
        addDisposable( RetrofitManager.apiService.login(params)
                .compose(RxUtils.io2main())
                .subscribeWith(object : ResultObserver<User>(){
                    override fun success(data: BaseResponse<User>) {
                        mView?.hideProgress()
                        mView?.loginSucess(data.data)
                    }

                    override fun failure(statusCode: Int, apiErrorModel: ApiErrorModel) {
                        mView?.hideProgress()
                        mView?.loginFailed(apiErrorModel.message)
                    }
                }))
    }
}